package license;

public class ByteM {

	public static byte[] subbyte(byte data[],int start,int end) {
		byte buf[] = new byte[end-start];
		for (int i = start;i < end;i++) {
			buf[i-start] = data[i];
		}
		return buf;
	}

	public static byte[] subbyte(byte data[],int start) {
		return subbyte(data,start,data.length);
	}

	public static byte[] concat(byte a1[],byte a2[],Integer limit_1,Integer limit_2) {
		if (limit_1 == null || limit_1.intValue() > a1.length) {
			limit_1 = new Integer(a1.length);
		}
		if (limit_2 == null || limit_2.intValue() > a2.length) {
			limit_2 = new Integer(a2.length);
		}
		byte bytes[] = new byte[limit_1.intValue()+limit_2.intValue()];
		for (int i = 0;i < limit_1.intValue();i++) {
			bytes[i] = a1[i];
		}
		for (int i = 0;i < limit_2.intValue();i++) {
			bytes[limit_1.intValue()+i] = a2[i];
		}
		return bytes;
	}

	public static byte[] concat(byte a1[],byte a2[]) {
		return concat(a1,a2,null,null);
	}
	
	public static boolean equals(byte a[],byte b[]) {
		if (a.length != b.length) return false;
		for (int i = 0;i < a.length;i++) {
			if (a[i] != b[i]) return false;
		}
		return true;
	}

	public static int indexOf(byte []a,byte []b) {
		boolean check = true;
		if (a.length < b.length) return -1;
		for (int i = 0;i < a.length-b.length+1;i++) {
			if (a[i] == b[0]) {
				check = true;
				for (int e = 0;e < b.length;e++) {
					if (a[i+e] != b[e]) {
						check = false;
					}
				}
				if (check) return i;
			}
		}
		return -1;
	}

	public static int lastIndexOf(byte []a,byte []b) {
		boolean check = true;
		if (a.length < b.length) return -1;
		for (int i = a.length-b.length;i >= 0;i--) {
			if (a[i] == b[0]) {
				check = true;
				for (int e = 0;e < b.length;e++) {
					if (a[i+e] != b[e]) {
						check = false;
					}
				}
				if (check) return i;
			}
		}
		return -1;
	}
	
	public static boolean startsWith(byte a[],byte b[]) {
		if (a.length < b.length) return false;
		for (int i = 0;i < b.length;i++) {
			if (a[i] != b[i]) return false;
		}
		return true;
	}

	public static byte[] replaceAll(byte a[],byte b[],byte c[]) {
		try {
			if (indexOf(b,c)!=-1) {
				throw new Exception("Neverendnig loop detected. " + new String(b) + " " + new String(c));
			}
			while (indexOf(a,b) != -1) {
				a = concat(concat(subbyte(a,0,indexOf(a,b)),c),subbyte(a,indexOf(a,b)+b.length));
			}
			return a;
		} catch (Exception exception) {
			return null;
		}
	}

	public static String toString(byte bytes[]) {
		String str = "[";
		for (int i = 0;i < bytes.length;i++) {
			str += bytes[i] + ",";
		}
		if (str.length() > 1) {
			return str.substring(0,str.length() - 1) + "]";
		} else {
			return str + "]";
		}
	}
	
	public static byte[] trim(byte data[]) {
		if (ByteM.lastIndexOf(data,new byte[]{(byte)' '}) != -1) {
			return ByteM.subbyte(data,0,ByteM.lastIndexOf(data,new byte[]{(byte)' '}));
		} else {
			return data;
		}
	}
	
	public static int byteToNum(byte []data,int start,int end) {
		int num = 0;
		for (int i = start;i < end;i++) {
			num+= ((data[i]&0xff) << ((end-start-i-1)*8));
		}
		return num;
	}
	
	public static byte[] numToByte(int number,int shifts) {
		byte []data = new byte[shifts];
		for (int i=0;i < shifts;i++) {
			data[i] = (byte)(number >> ((shifts-i-1)*8));
		}
		return data;
	}
	
}
